//
//  NewWithTableViewCell.m
//  SunXi
//
//  Created by dllo on 15/12/4.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "NewWithTableViewCell.h"

@implementation NewWithTableViewCell{
    UIImageView *_userimageView;
    UILabel *_messlabel;
    UIButton *_AddFriend;
    UIButton *_refuseFriend;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createData];
    }
    return self;
}
- (void)createData{
    _userimageView = [[UIImageView alloc]init];
    [SCV addSubview:_userimageView];
    
    self.username = [[UILabel alloc]init];
    [SCV addSubview:self.username];
    
    _messlabel = [[UILabel alloc]init];
    [SCV addSubview:_messlabel];
    
    _AddFriend = [UIButton buttonWithType:UIButtonTypeSystem];
    [SCV addSubview:_AddFriend];
    
    _refuseFriend = [UIButton buttonWithType:UIButtonTypeSystem];
    [SCV addSubview:_refuseFriend];
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [_userimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(35, 35));
        make.top.equalTo(SCV).offset(5);
        make.left.equalTo(SCV).offset(20);
    }];
    _userimageView.image = [UIImage imageNamed:@"haoyou"];
    
    [self.username mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_userimageView);
        make.left.equalTo(_userimageView.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(200, 20));
        
    }];
    self.username.text = self.model.name;
    
    [_messlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userimageView.mas_bottom).offset(10);
        make.left.equalTo(_userimageView);
        make.right.equalTo(_AddFriend.mas_left);
        make.bottom.equalTo(SCV);
    }];
    _messlabel.numberOfLines = 0;
    _messlabel.text = self.model.mess;
    
    
    [_refuseFriend mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(SCV).offset(-10);
        make.centerY.equalTo(SCV);
        make.size.mas_equalTo(CGSizeMake(50, 30));
    }];
    [_refuseFriend setTitle:@"拒绝" forState:0];
    _refuseFriend.backgroundColor = [UIColor redColor];
    [_refuseFriend setTintColor:[UIColor whiteColor]];
    [_refuseFriend addTarget:self action:@selector(refuseCliCk) forControlEvents:UIControlEventTouchUpInside];
    
    [_AddFriend mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_refuseFriend.mas_left).offset(-10);
        make.centerY.equalTo(SCV);
        make.size.mas_equalTo(CGSizeMake(50, 30));
    }];
    [_AddFriend setTitle:@"添加" forState:0];
    _AddFriend.backgroundColor = BUTTONCOLOR;
    [_AddFriend setTintColor:[UIColor whiteColor]];
    [_AddFriend addTarget:self action:@selector(_AddClick) forControlEvents:UIControlEventTouchUpInside];

}


/**
 *  拒绝添加好友
 */
- (void)refuseCliCk{
    EMError *error = nil;
    BOOL isSuccess = [[EaseMob sharedInstance].chatManager rejectBuddyRequest:self.model.name reason:@"111111" error:&error];
    if (isSuccess && !error) {
        NSLog(@"发送拒绝成功");
        [self.delegate removefriend:self.number];
        [ShowMytext showHUDTextWithTitle:@"已成功拒绝" View:[[SCV superview] superview]];
    }
}
/**
 *  同意添加好友
 */
- (void)_AddClick{
    EMError *error = nil;
    BOOL isSuccess = [[EaseMob sharedInstance].chatManager acceptBuddyRequest:self.model.name error:&error];
    if (isSuccess && !error) {
        NSLog(@"发送同意成功");
        [self.delegate removefriend:self.number];
        [ShowMytext showHUDTextWithTitle:@"添加成功" View:[[SCV superview] superview]];
    }
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
